﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace _3D_FileExplorer
{
    public partial class Settings : Form
    {
        public Settings()
        {
            InitializeComponent();
        }

        private void Settings_Load(object sender, EventArgs e)
        {
            // Tree Depth
            treeDepth.Value = Fe.par.defaultBranchLevel;
            labelTreeDepth.Text = treeDepth.Value.ToString();

            //User control
            if (Fe.par.enableCameraDollyIn)
            {
                checkBoxCameraDI.Checked = true;
            }
            else
            {
                checkBoxCameraDI.Checked = false;
            }
            if (Fe.par.keyBoardControl)
            {
                radioButtonKeyboard.Checked = true;
            }
            else
            {
                radioButtonMouse.Checked = true;
            }
            //Camera settings
            trackBarCameraSp.Value = (int)Fe.par.cameraSpeed;
            trackBarDiSpeed.Value = (int)Math.Round((double)((Fe.par.DollyInSpeed - 0.005f) / 0.001f));
            //other settings
            if (Fe.par.ContentRating)
            {
                checkBoxContentRating.Checked = true;
            }

            numericUpDownFaces.Value = Fe.par.cylinderFaces;
        }

        private void treeDepth_ValueChanged(object sender, EventArgs e)
        {
            labelTreeDepth.Text = treeDepth.Value.ToString();
            if (treeDepth.Value > 4)
            {
                toolTip1.Active = true;
                //treeDepthInfo.Text = "careful setting of this value can have a significant impact on computer performance";
                toolTip1.SetToolTip(treeDepth, " careful setting of this value can have a significant\n impact on computer performance");
            }
            else
            {
                toolTip1.Active = false;
            }
        }

        private void buttonSaveExit_Click_1(object sender, EventArgs e)
        {
            SaveAndSetParameters();
            Fe.form.Focus();
            Fe.form.Visible = true;
            this.Close();
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            SaveAndSetParameters();
        }
        private void SaveAndSetParameters()
        {
            //Tree depth
            Fe.par.defaultBranchLevel = treeDepth.Value;
            Fe.par.cylinderFaces = (Int32)numericUpDownFaces.Value;

            //User control
            if (checkBoxCameraDI.Checked)
            {
                Fe.par.enableCameraDollyIn = true;
            }
            else
            {
                Fe.par.enableCameraDollyIn = false;
            }

            if (radioButtonKeyboard.Checked)
            {
                Fe.par.keyBoardControl = true;
                DrawGraphicsTree.enableCameraAnimation = true;
            }
            else
            {
                Fe.par.keyBoardControl = false;
            }
            //Camera settings
            Fe.par.cameraSpeed = trackBarCameraSp.Value;
            //Other set.
            if (checkBoxContentRating.Checked)
            {
                Fe.par.ContentRating = true;
            }
            else
            {
                Fe.par.ContentRating = false;
            }
            Parameters.SerializeToXML(Fe.par);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(delegate() { OpenFile(Environment.CurrentDirectory + @"/XMLfiles/contentRating.xml"); });
            t.Start();
        }

        public static void OpenFile(string path)
        {
            try
            {
                System.Diagnostics.Process.Start("notepad.exe", path);
            }
            catch (Exception e)
            {
            }
        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void radioButtonKeyboard_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButtonKeyboard.Checked)
            {
                checkBoxCameraDI.Checked = true;
                checkBoxCameraDI.Enabled = false;
            }
            else
            {
                checkBoxCameraDI.Enabled = true;
            }
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }


        private void numericUpDownFaces_ValueChanged(object sender, EventArgs e)
        {

        }

        private void Settings_FormClosed(object sender, FormClosedEventArgs e)
        {
            Fe.form.Focus();
            Fe.form.Visible = true;
        }
    }
}
